package com.sskj.gocoin.v.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.base.App;
import com.sskj.common.util.SPUtil;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.adapter.slimadapter.IViewInjector;
import com.sskj.gocoin.adapter.slimadapter.SlimAdapter;
import com.sskj.gocoin.adapter.slimadapter.SlimInjector;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.component.DaggerUserDataComponent;
import com.sskj.gocoin.m.bean.FabiPublishListBean;
import com.sskj.gocoin.mvchelper.ModelRx2DataSource;
import com.sskj.gocoin.p.fragment.FabiRecordPresenter;
import com.sskj.gocoin.v.activity.FabiSellDetail2Activity;
import com.sskj.gocoin.v.activity.MainActivity;
import com.sskj.gocoin.v.base.BaseFragment;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

public class FabiRecordFragment extends BaseFragment<MainActivity, FabiRecordPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private SlimAdapter<FabiPublishListBean> slimAdapter;
    private MVCCoolHelper<List<FabiPublishListBean>> mvcCoolHelper;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fabi_record;
    }

    @Override
    public FabiRecordPresenter getPresenter() {
        return new FabiRecordPresenter();
    }

    public static FabiRecordFragment newInstance() {
        Bundle args = new Bundle();
        FabiRecordFragment fragment = new FabiRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }
//
//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser) {
//            if (mvcCoolHelper != null) {
//                mvcCoolHelper.refresh();
//            }
//        }
//    }

    @Override
    public void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        HashMap<String, String> statusMap = new HashMap<>();
        statusMap.put("1", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment2));
        statusMap.put("2", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment4));
        statusMap.put("3", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment6));
        statusMap.put("4", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment8));
        statusMap.put("5", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment10));
        statusMap.put("6", App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment12));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 20))
                .setDividerColor(ContextCompat.getColor(getContext(), R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.recy_item_fabi_record, new SlimInjector<FabiPublishListBean>() {
            @Override
            public void onInject(FabiPublishListBean data, IViewInjector injector) {

                injector
                        .text(R.id.tv_order_number, data.getOrder_num())
                        .text(R.id.tv_order_status, statusMap.get(data.getStatus()))
                        .text(R.id.tv_order_type, data.isBuyer() ? App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment13) : App.INSTANCE.getString(R.string.fabimineJavaFabiRecordFragment14))
                        .textColor(R.id.tv_order_type, data.isBuyer() ? ContextCompat.getColor(getActivity(), R.color.market_green) : ContextCompat.getColor(getActivity(), R.color.market_red))
                        .text(R.id.tv_order_price, data.getPrice())
                        .text(R.id.tv_order_amount, data.getTotal_num())
                        .text(R.id.tv_deal_money, data.getTotal_price())
                        .text(R.id.tv_order_date, data.getAdd_time());
            }
        });
//        ItemClickSupport.addTo(recyclerView)
//                .setOnItemClickListener((recyclerView, position, v) -> {
//                    FabiSellDetailActivity.start(getActivity(), slimAdapter.getDataItem(position).getOrder_num(), isBuy);
//                });
        slimAdapter.attachTo(recyclerView).updateData(new ArrayList());
//        Flowable.rangeLong(0, 8)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));
//        ModelRx2DataSource<FabiListBean> dataSource =
//                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FabiListBean>)
//                        page -> mPresenter.getFabiList(page, Constants.PAGE_SIZE, isBuy), Constants.PAGE_SIZE);
//        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
//        mvcCoolHelper.setDataSource(dataSource);
//        mvcCoolHelper.setAdapter(slimAdapter);
//        mvcCoolHelper.refresh();

        ModelRx2DataSource<FabiPublishListBean> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<FabiPublishListBean>)
                        page -> mPresenter.getData(page));
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    String account = SPUtil.get(SPConfig.ACCOUNT, "");
                    FabiSellDetail2Activity.start(getActivity(), slimAdapter.getDataItem(position).getOrder_num(), account.equals(slimAdapter.getDataItem(position).getGou_account()));
                });
    }
}
